/*     */ package com.wzh.service.bookinfo.chapter;
/*     */ 
/*     */ import com.wzh.bean.book.BookChapter;
/*     */ import com.wzh.constant.CommonConstant;
/*     */ import com.wzh.service.base.DaoSupport;
/*     */ import com.wzh.utils.CommonUtil;
/*     */ import com.wzh.utils.PropertiesUtil;
/*     */ import java.io.Serializable;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import javax.persistence.EntityManager;
/*     */ import javax.persistence.Query;
/*     */ import javax.servlet.ServletContext;
/*     */ import org.apache.struts2.ServletActionContext;
/*     */ import org.springframework.stereotype.Service;
/*     */ import org.springframework.transaction.annotation.Propagation;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Service
/*     */ @Transactional
/*     */ public class BookChapterServiceBean extends DaoSupport<BookChapter>
/*     */   implements BookChapterService
/*     */ {
/*     */   public List<String> getChapterUrlsByBookid(Integer bookid)
/*     */   {
/*  32 */     Query query = this.em.createNativeQuery("select resourceUrl from bookchapter where bookid=?1");
/*  33 */     query.setParameter(1, bookid);
/*     */ 
/*  35 */     List urls = query.getResultList();
/*  36 */     return urls;
/*     */   }
/*     */ 
/*     */   public void addChaptersByMap(Map<String, String> urlAndTite, Integer bookid, Integer siteid) {
/*  40 */     if ((urlAndTite != null) && (urlAndTite.size() > 0))
/*     */     {
/*  42 */       List urls = getChapterUrlsByBookid(bookid);
/*     */ 
/*  45 */       for (Map.Entry entry : urlAndTite.entrySet())
/*     */       {
/*  49 */         if ((urls == null) || (!urls.contains(entry.getKey())))
/*     */         {
/*  52 */           BookChapter chapter = new BookChapter();
/*  53 */           chapter.setResourceUrl((String)entry.getKey());
/*  54 */           chapter.setTitle((String)entry.getValue());
/*  55 */           chapter.setBookid(bookid);
/*  56 */           chapter.setSiteid(siteid);
/*  57 */           save(chapter);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   @Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
/*     */   public boolean find(Integer bookid, String resourceUrl)
/*     */   {
/*  67 */     Query query = this.em.createQuery("select count(o) from BookChapter o where o.bookid=?1 and o.resourceUrl=?2");
/*  68 */     query.setParameter(1, bookid);
/*  69 */     query.setParameter(2, resourceUrl);
/*  70 */     return ((Long)query.getSingleResult()).longValue() > 0L;
/*     */   }
/*     */ 
/*     */   public List<BookChapter> getChapters(Integer bookid, int firstindex, int maxresult, String order)
/*     */   {
/*  78 */     String sql = "select id,title from bookchapter where bookid=?1 ";
/*  79 */     if ((order != null) && (!"".equals(order))) {
/*  80 */       sql = sql + " order by " + order;
/*     */     }
/*     */ 
/*  84 */     Query query = this.em.createNativeQuery(sql);
/*  85 */     if (firstindex >= 0) {
/*  86 */       query.setFirstResult(firstindex);
/*  87 */       query.setMaxResults(maxresult);
/*     */     }
/*  89 */     query.setParameter(1, bookid);
/*     */ 
/*  91 */     List list = query.getResultList();
/*  92 */     List chapters = new ArrayList();
/*  93 */     for (Iterator localIterator = list.iterator(); localIterator.hasNext(); ) { Object obj = localIterator.next();
/*  94 */       Object[] objs = (Object[])obj;
/*  95 */       BookChapter chapter = new BookChapter();
/*  96 */       chapter.setId((Integer)objs[0]);
/*  97 */       chapter.setTitle((String)objs[1]);
/*  98 */       chapter.setBookid(bookid);
/*     */ 
/* 101 */       setReadurl(chapter);
/* 102 */       chapters.add(chapter);
/*     */     }
/*     */ 
/* 105 */     return chapters;
/*     */   }
/*     */ 
/*     */   public void setReadurl(BookChapter chapter) {
/* 109 */     PropertiesUtil properUtil = new PropertiesUtil("config.properties");
/* 110 */     if ("0".equals(properUtil.getProperty("urlwrite_tag")))
/*     */     {
/* 112 */       chapter.setReadurl("/home/chapter?chapterid=" + chapter.getId());
/*     */     }
/*     */     else {
/* 115 */       String urlStr = "/" + chapter.getBookid() + "/" + chapter.getId() + ".html";
/*     */ 
/* 117 */       chapter.setReadurl(urlStr);
/*     */     }
/*     */   }
/*     */ 
/*     */   @Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
/*     */   public void setPreAndPostUrl(BookChapter chapter)
/*     */   {
/* 125 */     PropertiesUtil properUtil = new PropertiesUtil("config.properties");
/*     */ 
/* 128 */     Query query = this.em.createNativeQuery("select id from bookchapter o where o.id>?1 and o.bookid=?2 limit 1");
/* 129 */     query.setParameter(1, chapter.getId());
/* 130 */     query.setParameter(2, chapter.getBookid());
/*     */ 
/* 132 */     List list = query.getResultList();
/* 133 */     int postId = 0;
/* 134 */     if ((list != null) && (list.size() > 0))
/*     */     {
/* 136 */       postId = ((Integer)list.get(0)).intValue();
/*     */     }
/*     */ 
/* 140 */     int preId = 0;
/* 141 */     query = this.em.createNativeQuery("select id from bookchapter o where o.id<?1 and o.bookid=?2 order by id desc limit 1");
/* 142 */     query.setParameter(1, chapter.getId());
/* 143 */     query.setParameter(2, chapter.getBookid());
/* 144 */     list = query.getResultList();
/* 145 */     if ((list != null) && (list.size() > 0)) {
/* 146 */       preId = ((Integer)list.get(0)).intValue();
/*     */     }
/*     */ 
/* 149 */     if ("0".equals(properUtil.getProperty("urlwrite_tag")))
/*     */     {
/* 151 */       chapter.setReadurl("/home/chapter?chapterid=" + chapter.getId());
/* 152 */       if (preId > 0) {
/* 153 */         chapter.setPreurl("/home/chapter?chapterid=" + preId);
/*     */       }
/* 155 */       if (postId > 0) {
/* 156 */         chapter.setPosturl("/home/chapter?chapterid=" + postId);
/*     */       }
/*     */     }
/*     */     else
/*     */     {
/* 161 */       chapter.setReadurl("/" + chapter.getBookid() + "/" + chapter.getId() + ".html");
/* 162 */       if (preId > 0) {
/* 163 */         chapter.setPreurl("/" + chapter.getBookid() + "/" + preId + ".html");
/*     */       }
/* 165 */       if (postId > 0)
/* 166 */         chapter.setPosturl("/" + chapter.getBookid() + "/" + postId + ".html");
/*     */     }
/*     */   }
/*     */ 
/*     */   public void delete(Integer bookid, Integer Chapterid)
/*     */   {
/* 176 */     String realFilePath = ServletActionContext.getServletContext().getRealPath(CommonConstant.TXTPATH) + 
/* 177 */       CommonConstant.DELIMITER + bookid + CommonConstant.DELIMITER + Chapterid + ".txt";
/* 178 */     CommonUtil.deleteFile(realFilePath);
/* 179 */     delete(new Serializable[] { Chapterid });
/*     */   }
/*     */ }

/* Location:           C:\work\apache-tomcat-7.0.55\webapps\ROOT\WEB-INF\classes\
 * Qualified Name:     com.wzh.service.bookinfo.chapter.BookChapterServiceBean
 * JD-Core Version:    0.6.1
 */